package com.example.config;

import com.example.mqtt.doman.dto.MqttBrokerDTO;
import com.example.mqtt.doman.dto.MqttClientOptionsDTO;
import com.example.mqtt.doman.enums.MqttProtocolEnum;
import com.example.mqtt.doman.enums.MqttUseEnum;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author sean
 * @version 1.3
 * @date 2023/1/18
 */

@Component
@ConfigurationProperties("mqtt")
public class MqttProperty {

    public static Map<MqttUseEnum, MqttClientOptionsDTO> conf;

    public static Boolean enable;

    public static String appId;

    public void setAppId(String appId) {
        MqttProperty.appId = appId;
    }

    public String getAppId() {
        return appId;
    }

    public void setEnable(Boolean enable) {
        MqttProperty.enable = enable;
    }

    public Boolean getEnable() {
        return enable;
    }

    public void setConf(Map<MqttUseEnum, MqttClientOptionsDTO> mqtt) {
        MqttProperty.conf = mqtt;
    }

    public Map<MqttUseEnum, MqttClientOptionsDTO> getConf() {
        return conf;
    }

    public static Boolean isEnable() {
        return Boolean.TRUE.equals(enable);
    }

    /**
     * Get the configuration options of the basic link of the mqtt client.
     * @return
     */
   public static MqttClientOptionsDTO getBasicClientOptions() {
        if (!conf.containsKey(MqttUseEnum.BASIC)) {
            throw new Error("Please configure the basic mqtt connection parameters first, otherwise application cannot be started.");
        }
        return conf.get(MqttUseEnum.BASIC);
    }


    public static String getMqttAddress(MqttClientOptionsDTO options) {
    /**
     * Splice the mqtt address according to the parameters of different clients.
     * @param options
     * @return
     */
        StringBuilder addr = new StringBuilder()
                .append(options.getProtocol().getProtocolAddr())
                .append(options.getHost().trim())
                .append(":")
                .append(options.getPort());
        if ((options.getProtocol() == MqttProtocolEnum.WS || options.getProtocol() == MqttProtocolEnum.WSS)
                && StringUtils.hasText(options.getPath())) {
            addr.append(options.getPath());
        }
        return addr.toString();
    }
    /**
     * Get the mqtt address of the basic link.
     * @return
     */
    public static String getBasicMqttAddress() {
        return getMqttAddress(getBasicClientOptions());
    }

    /**
     * Get the connection parameters of the mqtt client of the drc link.
     * @param clientId
     * @param username
     * @param age   The validity period of the token. unit: s
     * @param map   Custom data added in token.
     * @return
     */
    public static MqttBrokerDTO getMqttBrokerWithDrc(String clientId, String username, String password, Long age, Map<String, ?> map) {
        if (!conf.containsKey(MqttUseEnum.DRC)) {
            throw new RuntimeException("Please configure the drc link parameters of mqtt in the backend configuration file first.");
        }
        return MqttBrokerDTO.builder()
                .address(getMqttAddress(conf.get(MqttUseEnum.DRC)))
                .username(username)
                .password(password)
                .clientId(clientId)
                .expireTime(System.currentTimeMillis() / 1000 + age)
                .build();
    }

}
